<?php

namespace frontend\models;

use Yii;

/**
 * This is the model class for table "order_detail".
 *
 * @property string $detailId
 * @property string $productId
 * @property string $price
 * @property integer $productnum
 * @property string $orderId
 * @property string $createtime
 *
 * @property Product $product
 * @property ShopOrder $order
 */
class OrderDetail extends superModel {

	/**
	 * @inheritdoc
	 */
	public static function tableName() {
		return 'order_detail';
	}

	public static function getCTableName() {
		return 'order_detail';
	}

	/**
	 * @inheritdoc
	 */
	public function rules() {
		return [
				[['productId', 'productnum', 'orderId'], 'integer'],
				[['price'], 'number'],
				[['createtime'], 'safe'],
				[['productId'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['productId' => 'productId']],
				[['orderId'], 'exist', 'skipOnError' => true, 'targetClass' => ShopOrder::className(), 'targetAttribute' => ['orderId' => 'orderId']],
		];
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels() {
		return [
			'detailId' => '订单详情ID',
			'productId' => '商品',
			'price' => '商品价格',
			'productnum' => '商品数量',
			'orderId' => '订单',
			'createtime' => '创建时间',
		];
	}

	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getProduct() {
		return $this->hasOne(Product::className(), ['productId' => 'productId']);
	}

	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getOrder() {
		return $this->hasOne(ShopOrder::className(), ['orderId' => 'orderId']);
	}

	public function beforeSave($insert) {
		if (parent::beforeSave($insert)) {
			if ($this->isNewRecord) {
				$this->createtime = $this->getDateTime();
			} 
			return TRUE;
		} else {
			return FALSE;
		}
	}

}
